
$(document).ready(function(){
    announcement.postAnnouncement(".btnAnnouncement"); // save to the database;
    announcement.getAnnouncementPost(); // load announcement from the database
});

// announcement js functions
announcement = {
    // Post Announcement
    postAnnouncement : function(elements){
        $("body").on("click",elements,function(){
            // Replacing comment if it is contains a bad words
                announcement = $(".announcement").val().replace(new RegExp("gago","g"),"****").replace(new RegExp("peste","g"),"****").
                replace(new RegExp("putangina mo","g"),"****").replace(new RegExp("putanginamo","g"),"****").replace(new RegExp("fuck","g"),"****").
                replace(new RegExp("ass hole","g"),"****");
            //announcement = announcement.val();
            action = "getAnnouncementPost";
            $("#loading").show();
            $(elements).attr("disabled",true);
            if(announcement!=""){
                $.ajax({
                    type: "POST",
                    url : "/announcement/"+announcement,
                    success : function(e){
                        showNotification({
                            message: "Please type your prefered announcement above.",
                                type: "success",
                           autoClose: true,
                           duration: 3
                        });
                        $(".announcement").val(null);
                        $(".postHere").prepend(e);
                        $(elements).attr("disabled",false);
                    }
                })
            }else{
                showNotification({
                    message: "Please type your prefered announcement above.",
                        type: "error",
                   autoClose: true,
                   duration: 3
                });
                $("#loading").hide();
                $(elements).attr("disabled",false);
            }
        });
    },
    getAnnouncementPost : function(elements){
        $.ajax({
            type : "GET",
            url : "/announcement",
            success : function(e){
                $(".postHere").html(e);
            }
        });
    }
}

